
package com.monlyu.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.FastDateFormat;

/**
 * 日期操作函数
 * 
 * @author monlyu
 * @since 2009-12-25
 */
public class DateUtil {

    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIMENOS_PATTERN = "yyyy-MM-dd HH:mm";
    public static final String COMPACT_TRIM_SECOND_PATTERN = "yyyyMMdd HH:mm";
    public static final String COMPACT_DATE_PATTERN = "yyyyMMdd";

    /**
     * @return 当前日期
     */
    public static Date now(){
        return new Date();
    }

    /**
     * 当前日期的字符串
     */
    public static String now2Str(){
        return format(now());
    }

    public static Date nowZero(){
        return parse(now2Str(), DATE_PATTERN);
    }

    /**
     * 当前时间的字符串
     */
    public static String nowtime2Str(){
        return format(now(), DATETIME_PATTERN);
    }

    /**
     * 格式化时间为字符串格式<br>
     * 默认为 yyyy-MM-dd
     * 
     * @param date
     * @return 经过格式化之后的日期
     */
    public static String format(Date date){
        return format(date, DATE_PATTERN);
    }

    /**
     * 格式化时间为字符串格式<br>
     * 
     * @param date
     *            需要格式化的时间
     * @param pattern
     *            匹配的的样式
     * @return 日期对应的字串
     */
    public static String format(Date date, String pattern){
        if (date == null) {
            return null;
        }
        return FastDateFormat.getInstance(pattern).format(date);
    }

    public static Date parse(String value, String parnet){
        if (StringUtil.isTrimEmpty(value)) {
            return null;
        }
        DateFormat parser = new SimpleDateFormat(parnet);
        try {
            return parser.parse(value);
        }
        catch (ParseException e) {
            throw new IllegalArgumentException("格式化日期格式的时候出错:" + value + "@" + parnet);
        }
    }

    public static Date getStartTimeOfDate(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    public static Date getEndTimeOfDate(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    /**
     * 向后推移几日
     * 
     * @param date
     *            日期
     * @param i
     *            天数
     * @since 2010-3-4
     */
    public static Date addDay(Date date, int i){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, i);
        return calendar.getTime();
    }

    public static String getmilltime(){
        return format(DateUtil.now(), DATETIME_PATTERN);
    }

    /**
     * 获取天数
     * 
     * @param d1 开始时间
     * @param d2 结束时间
     * @return
     */
    public static int dayBetween(Date d1, Date d2){
        long dt1 = d1.getTime();
        long dt2 = d2.getTime();
        int days = (int) ((dt2 - dt1) / (24 * 3600 * 1000));
        return days;
    }
}
